package com.tianhui.consignortwofei.mvp.ui.activity;

import android.view.View;
import android.widget.GridView;
import android.widget.TextView;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.tianhui.consignortwofei.BuildConfig;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseActivity;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.enty.ShenHeTongJiBean;
import com.tianhui.consignortwofei.mvp.ui.adapter.ShenHeTongJiAdapter;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/28
 * @description
 **/
public class ShenHeTongJiActivity extends BaseActivity {

    @BindView(R.id.gv_tongji)
    GridView gv_tongji;

    @BindView(R.id.tv_qishi)
    TextView tv_qishi;
    @BindView(R.id.tv_zhongzhi)
    TextView tv_zhongzhi;
    @BindView(R.id.tv_sousuo)
    TextView tv_sousuo;



    private DriverModel driverModel;
    private List<ShenHeTongJiBean> tongJilist =new ArrayList<>();
    private ShenHeTongJiAdapter mainShopListAdapter;
    private String startDate="";
    private String endDate="";


    @Override
    public int getLayoutId() {
        return R.layout.activity_shenhe_hetong;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {
        setTitle("统计");

        driverModel=new DriverModel();

        mainShopListAdapter = new ShenHeTongJiAdapter(tongJilist, this);
        gv_tongji.setAdapter(mainShopListAdapter);

        String year = DateUtil.getYear();
        tv_qishi.setText("起始时间："+year);
        tv_zhongzhi.setText("终止时间："+year);
        startDate=year;
        endDate=year;
        requestDriverList();



    }

    @Override
    public BaseFragment createFragment() {
        return null;
    }

    private TimePickerView mEndDatePickerView;
    @OnClick(R.id.tv_qishi)
    public void qishiTime() {
        if (mEndDatePickerView == null) {
            mEndDatePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    tv_qishi.setText("起始时间："+string);
                    startDate=string;
//                    mDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择起始日期")
                    .build();
        }
        mEndDatePickerView.show();
    }


    private TimePickerView mEndDatePickerViewTwo;
    @OnClick(R.id.tv_zhongzhi)
    public void zhongzhiTime() {
        if (mEndDatePickerViewTwo == null) {
            mEndDatePickerViewTwo = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String string = DateUtil.getStandardTime(date.getTime(), "yyyy-MM-dd");
                    tv_zhongzhi.setText("终止时间："+string);
                    endDate=string;
//                    mDateClickItemView.setContent(string);
                }
            })
                    .setTitleText("请选择终止日期")
                    .build();
        }
        mEndDatePickerViewTwo.show();
    }

    @OnClick(R.id.tv_sousuo)
    public void souSuo() {
        requestDriverList();
    }


    public void requestDriverList() {

        Map<String, String> map = new HashMap<>();
//        map.put("page", page+"");
//        map.put("limit", "15");
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        driverModel.shenHeTongJi(this, map, true, bindLifecycle(), new ObserverResponseListener<CommonResponse<List<ShenHeTongJiBean>>>() {
            @Override
            public void onNext(CommonResponse<List<ShenHeTongJiBean>> commonResponse) {
                if (!isFinishing() && !isDestroyed()) {
                    if (commonResponse.getCode() == 20000) {
                        List<ShenHeTongJiBean> list = commonResponse.getData();


                        tongJilist.clear();
                        tongJilist.addAll(list);
                        mainShopListAdapter.notifyDataSetChanged();

//                        List<QuotationListBean.QuotationRecords> resultList = new ArrayList<>();=
//                        dataList.addAll(list);
//                        if (list != null && list.size() > 0) {
//                            showDataList(list);
//                        }
//                        else {
//                            showEmptyView();
//                        }
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }



}
